#include "stdafx.h"

#include "MyControls/BrushTool.h"	  
#include "MyControls/MaskEditor.h"

namespace My {

IMPLEMENT_DYNAMIC(BrushTool, Tool)

BrushTool::BrushTool(MaskEditor & owner)
	: CircleTool(owner.GetBench()), m_MaskEditor(owner)
{
}

BEGIN_MESSAGE_MAP(BrushTool, CCmdTarget)
	//{{AFX_MSG_MAP(BrushTool)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void BrushTool::Synchronize()
{
	m_MaskEditor.Synchronize("Brush");
}

void BrushTool::Select(CPoint p, int MouseState)
{
	if ((MouseState & LButtonDown) && (MouseState & RButtonDown))
		return;

	const CRect rBrush = GetWireRect(p);
	rect<int> r = GetBench().ClientToBench(rBrush);

	Extent dim = m_MaskEditor.GetBuffer().dim();
	rect<int> bounds(0, 0, dim.sx, dim.sy);
	if (!intersect(bounds, r, &r)) {
		//? if (m_ShowWire && m_WireVisible && !m_Hidden)
		//?	RedrawWire();
		return;
	}

	point<int> tl = GetWireBenchTopLeft(p);

	if ((MouseState & LButtonDown))
		img::combine(m_MaskEditor.GetBuffer(),
			tl.x, tl.y, GetDoubleSize(), GetDoubleSize(),
			GetCircleBitmap(), 0, 0, img::bit_or());
	else
		img::combine(m_MaskEditor.GetBuffer(),
			tl.x, tl.y, GetDoubleSize(), GetDoubleSize(),
			GetCircleBitmap(), 0, 0, img::bit_and_not());
}

} //namespace My
